package com.buka.goods.service.impl;


import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.buka.goods.mapper.ProductMapper;
import com.buka.goods.service.ProductService;
import com.buka.server.pojo.Product;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;
    @Override
    public List<Product> findAll() {
        Product product = new Product();
        product.setIsDelete(0);
        return productMapper.select(product);
    }

    @Override
    public List<Product> findList(Product product) {
        Example example = new Example(Product.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("isDelete",0);
        if (product.getName() != null) {
            criteria.andLike("name","%"+product.getName()+"%");
        }
        if (product.getId() != null) {
            criteria.andEqualTo("id",product.getId());
        }
        if (product.getShopName() != null) {
            criteria.andLike("shopName","%"+product.getShopName()+"%");
        }
        if (product.getStatus() != null) {
            criteria.andEqualTo("status",product.getStatus());
        }
        if (product.getSalesModel()!= null){
            criteria.andEqualTo("salesModel",product.getSalesModel());
        }
        if (product.getType() != null){
            criteria.andEqualTo("type",product.getType());
        }
        return productMapper.selectByExample(example);
    }

    @Override
    public Page<Product> findAllPage(Integer page, Integer size) {
        PageHelper.startPage(page,size);
        return (Page<Product>) this.findAll();
    }

    @Override
    public Page<Product> findListPage(Integer page, Integer size, Product product) {
        PageHelper.startPage(page,size);
        return (Page<Product>) this.findList(product);
    }

    @Override
    public Integer add(Product product) {
        product.setId(IdWorker.getId());
        return productMapper.insertSelective(product);
    }

    @Override
    public Integer update(Product product) {
        product.setUpdateTime(new Date());
        return productMapper.updateByPrimaryKeySelective(product);
    }

    @Override
    public Integer delete(Long id) {
        Product product = new Product();
        product.setId(id);
        product.setIsDelete(1);
        return productMapper.updateByPrimaryKeySelective(product);
    }

    @Override
    public Integer status(Long id, Integer status) {
        Product product = new Product();
        product.setId(id);
        product.setStatus(status);
        return productMapper.updateByPrimaryKeySelective(product);
    }
}